// pages/order/delivery/delivery.js
const app = getApp()
var util = require('../../../utils/util.js')
var http = require('../../../utils/server.js')


Page({
  data: {
    none: "none",
    user: {
    },
    logCompanysI: [],
    logCompanys: [],
    userList: [],
    logCompanysData: [],
  },

  onLoad: function(options) {
    var that = this;

    console.info(options.id)
    that.data.id = options.id;
    http.post({
      url: "Order/getInfo",
      order_id: options.id,
      // status: status,
    }, function(data) {
      that.setData({
        userList: data.address,
        user: wx.getStorageSync('userInfo')
      });
    });


    http.post({
      url: "Address/getAllExpress",
      type: ''
    }, function(data) {
      console.log(data)
      var companys = [];
      for (var i = 0; i < data.length; i++) {
        companys.push(data[i].name);
      }
      that.data.logCompanysData = data;
      that.setData({
        logCompanys: companys
      })
    })


  },
  scan: function(event) {
    var that = this;
    console.log(event.currentTarget.dataset.index)
    wx.scanCode({
      success: (res) => {
        console.log(res)
        that.data.userList[event.currentTarget.dataset.index].no = res.result;
        this.setData({
          userList: that.data.userList
        })
      }
    })
  },
  warning: function() {
    this.setData({
      none: ""
    });
  },
  closeTips: function() {
    this.setData({
      none: "none"
    });
  },
  input: function(e) {
    console.info(e);
    var index = e.target.dataset.index;
    var value = e.detail.value;
    this.data.userList[index].no = value;
    console.info(this.data.userList)
  },
  change: function(event) {
    console.info(event.detail.value);
    this.data.logCompanysI[event.currentTarget.dataset.index] = parseInt(event.detail.value);
    this.setData({
      logCompanysI: this.data.logCompanysI
    })
  },
  confirm: function() {
    var that = this;
    wx.showModal({
      title: '操作提示',
      content: '确定发货吗?',
      success: function(res) {
        if (res.confirm) {
          var companyI = that.data.logCompanysI;
          var codes = [];
          for (var i in companyI) {
            codes.push(that.data.logCompanysData[companyI[i]].code);
          }
          var numbers = [];
          for (var i in that.data.userList) {
            numbers.push(that.data.userList[i].no);
          }
          console.info(codes, numbers);
          http.post({
            url: "Order/sendGoods",
            codes: codes,
            numbers: numbers,
            order_id: that.data.id,
          }, function(data) {
            util.showSuccess("发货成功", function() {
              wx.navigateBack({
                delta: 1
              })
            })
          });
        }
      }
    })
  }
})